﻿using Candy.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Candy
{
    public partial class RegistrationWindow : ChildWindow
    {
        public RegistrationWindow()
        {
            InitializeComponent();
            if (DataContext is IPasswordRequester)
                (DataContext as IPasswordRequester).PasswordAccessor = () => { return passwordBox.Password; };
            if (DataContext is IConfirmedPasswordRequester)
                (DataContext as IConfirmedPasswordRequester).PasswordConfirmationAccessor = () => { return passwordConfirmBox.Password; };

            if (LayoutRoot.DataContext is IPasswordRequester)
                (LayoutRoot.DataContext as IPasswordRequester).PasswordAccessor = () => { return passwordBox.Password; };
            if (LayoutRoot.DataContext is IConfirmedPasswordRequester)
                (LayoutRoot.DataContext as IConfirmedPasswordRequester).PasswordConfirmationAccessor = () => { return passwordConfirmBox.Password; };
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Candy.Infrastructure.ValidationHelper.ForceValidation(LayoutRoot);
        }



        //  public CaptureSource captureSource;

        private void buttonWebcam_Click(object sender, RoutedEventArgs e)
        {
            // Determine if we have access to webcams
            if (!CaptureDeviceConfiguration.AllowedDeviceAccess)
            {
                // if not, request access
                if (!CaptureDeviceConfiguration.RequestDeviceAccess())
                {
                    // denied! 
                    return;
                }
            }
            //StartWebCam();


            WebcamChildWindow w = new WebcamChildWindow();
            w.Closed += new EventHandler(w_Closed);
            w.Show();
        }

        void w_Closed(object sender, EventArgs e)
        {
            WebcamChildWindow w = sender as WebcamChildWindow;
            if (w.DialogResult == true)
            {
                //set the captured photo
                avatarImage.Source = w.CapturedPhoto;
            }
        }


        /// <summary>
        /// Open a file from hard disk
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOpenFile_Click(object sender, RoutedEventArgs e)
        {
            Stream profileImageStream = null;
            OpenFileDialog openWindow = new OpenFileDialog();

            openWindow.Filter = "Jpeg image files (*.jpg)|*.jpg";
            if (openWindow.ShowDialog() == true)
            {
                FileInfo targetFile = openWindow.File;

                if (IsImageFile(targetFile.Extension))
                {
                    profileImageStream = targetFile.OpenRead();
                    // Saving a copy of the image's stream provided for future byte[] generation
                    //  lastStreamProfilePicture = profileImageStream;

                    System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
                    image.SetSource(profileImageStream);

                    // Filling the XAML image control
                    avatarImage.Source = image;
                }
            }
        }

        private void avatarImage_Drop(object sender, DragEventArgs e)
        {
            // Get FileInfo array from DragEventArgs
            IDataObject dataObject = e.Data;
            var files = (FileInfo[])dataObject.GetData(DataFormats.FileDrop);

            // taking arbitrary the first file of the dropped files collection
            FileInfo file = files[0];

            if (file != null && IsImageFile(file.Extension))
            {
                Stream stream = file.OpenRead();
                string name = file.Name;


                BitmapImage droppedImage = new BitmapImage();
                droppedImage.SetSource(stream);

                avatarImage.Source = droppedImage;

            }
        }

        // Checking if image is in JPG format
        private bool IsImageFile(string extension)
        {
            return (extension.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase)
                || extension.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase));
        }

  


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

